package com.wc.alorithm_luogu._动态规划.P1048;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author congge
 * @Date 2023/9/27 18:34
 * @description 采药
 * 动态规划难题
 * https://www.luogu.com.cn/problem/P1048
 */
public class Main {
    public static void main(String[] args) {
        Input scan = new Input();
        int t = scan.nextInt();
        int m = scan.nextInt();

        Herb[] herbs = new Herb[m];

        for (int i = 0; i < m; i++) {
            herbs[i] = new Herb();
            herbs[i].time = scan.nextInt();
            herbs[i].value = scan.nextInt();
        }
        Comparator<Herb> comparator = new Comparator<Herb>() {
            @Override
            public int compare(Herb o1, Herb o2) {
                return o1.time - o2.time;
            }
        };
        Arrays.sort(herbs, comparator);
        int[][] dp = new int[m][t + 1];
        for (int i = herbs[0].time; i <= t; i++) {
            dp[0][i] = herbs[0].value;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j <= t; j++) {
                if (j >= herbs[i].time) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - herbs[i].time] + herbs[i].value);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        System.out.println(dp[m - 1][t]);
    }

    static class Herb {
        public int time;
        public int value;
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
